package com.kky.pp.view

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.custom.base.base.BaseRecyclerAdapter
import com.custom.base.view.SuperViewHolder
import com.kky.pp.R
import com.kky.pp.bean.TOListBean
import com.kky.pp.databinding.ViewTOBinding
import com.kky.pp.entity.UserProfileEntity
import com.kky.pp.popup.TOListPopup
import com.kky.pp.util.GlideUtil

class ProfileInfoTurnOnsView(context: Context, val attrs: AttributeSet?) :
    FrameLayout(context, attrs) {

    //private var view: View? = null
    //var moreView: View? = null
    var adapter: Adapter? = null

//    var viewTurnOnsBg: ConstraintLayout? = null
//    var viewMore: FrameLayout? = null
//    var turnOnsRecyclerView: RecyclerView? = null
//    var viewLine: View? = null
    private var itemBackground = -1
    var isShowOriginalBg : Boolean = false

    private var binding:ViewTOBinding? = null

    init {
        binding = ViewTOBinding.inflate(LayoutInflater.from(context),this,true)
        //view = LayoutInflater.from(context).inflate(R.layout.view_t_o, this)
        initRecyclerView()
        initListener()
    }

    fun initListener() {
        //moreView = view?.findViewById(R.id.textTurnOns)
    }

    fun setBg(){
        binding?.viewProfileInfoTurnOns?.setBackgroundResource(R.color.Transparent)
        binding?.viewMore?.isVisible = false
    }

    fun setPadding(){
        binding?.viewMian?.setPadding(0, 0, 0, 0)
    }

    fun setBgNull(){
        binding?.viewProfileInfoTurnOns?.background=null
    }

    fun setCLick(context: FragmentActivity,entity: UserProfileEntity,callback: (ArrayList<TOListBean>) -> Unit){
        binding?.textTurnOns?.setOnClickListener {
            TOListPopup.openDialog(context, entity.data.turnOnsList)
                .setOnClickSaveListener(object :
                    TOListPopup.OnClickSaveListener {
                    override fun clickSave(dataList: ArrayList<TOListBean>) {
                        entity.data.turnOnsList = dataList
                        callback(dataList)
                    }

                })
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(dataList: ArrayList<TOListBean>) {
        binding?.turnOnsEmpty?.isVisible = dataList.isEmpty()
        binding?.turnOnsRecyclerView?.isVisible = dataList.size > 0
        binding?.viewLine?.isVisible = dataList.size > 0
        adapter?.clear()
        adapter?.dataList = dataList
        adapter?.notifyDataSetChanged()
    }

    private fun initRecyclerView() {
        val obtainStyledAttributes =
            context.obtainStyledAttributes(attrs, R.styleable.ProfileInfoTurnOnsView)
        val layoutResid = obtainStyledAttributes.getResourceId(
            R.styleable.ProfileInfoTurnOnsView_itemLayout,
            R.layout.item_profile_turn_ons
        )
        isShowOriginalBg = obtainStyledAttributes.getBoolean(R.styleable.ProfileInfoTurnOnsView_isShowOriginalBg,false)
        itemBackground = obtainStyledAttributes.getResourceId(
            R.styleable.ProfileInfoTurnOnsView_itemBackground,
            -1
        )

//        viewMore = view?.findViewById(R.id.viewMore)
//        viewLine = view?.findViewById(R.id.viewLine)
//        viewTurnOnsBg = view?.findViewById(R.id.viewProfileInfoTurnOns)
//        turnOnsRecyclerView = view?.findViewById<RecyclerView>(R.id.turnOnsRecyclerView)
        binding?.turnOnsRecyclerView?.layoutManager = GridLayoutManager(context, 2)
        adapter = Adapter(context as Activity, arrayListOf(), layoutResid,isShowOriginalBg)
        adapter?.itemBackground = itemBackground
        binding?.turnOnsRecyclerView?.adapter = adapter
    }

    class Adapter(
        val activity: Activity,
        listModel: ArrayList<TOListBean>,
        val layoutResid: Int,
        val isShowOriginalBg:Boolean? = false
    ) :
        BaseRecyclerAdapter<TOListBean>(listModel, activity) {

        var itemBackground: Int = -1
        override val layoutId: Int
            get() = layoutResid
        var top: Int = 0

        private var onItemClickListener: OnItemClickListener? = null
        fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
            this.onItemClickListener = onItemClickListener
        }

        override fun onBindItemHolder(
            position: Int,
            holder: SuperViewHolder,
            model: TOListBean,
            payloads: List<Any>?
        ) {
            val ivTurnOns = holder.getView<ImageView>(R.id.ivTurnOns)
            val tvTurnOnsTitle = holder.getView<TextView>(R.id.tvTurnOnsTitle)
            val tvTurnOnsContent = holder.getView<TextView>(R.id.tvTurnOnsContent)
            val turnOnsContainer = holder.getView<ConstraintLayout>(R.id.turnOnsContainer)
            GlideUtil.setImageView(model.imageUrl, ivTurnOns, placeholderImageId = 0)
            tvTurnOnsTitle.text = model.title
            tvTurnOnsContent.text = model.content
            turnOnsContainer.setBackgroundResource(
                if (itemBackground != -1) {
                    itemBackground
                } else {
                    if (model.selected == 0 || isShowOriginalBg == true) R.drawable.shape_3c475d_radius_12 else R.drawable.shape_4634a9_f63669_radius_12
                }
            )
            if (top != 0 && position < 2) {
                holder.getView<ConstraintLayout>(R.id.itemViewMain).setPadding(0, top, 0, 0)
            } else {
                holder.getView<ConstraintLayout>(R.id.itemViewMain).setPadding(0, 0, 0, 0)
            }
            holder.setOnClickListener {
                onItemClickListener?.onItemClick(position, holder, model)
            }
        }

        interface OnItemClickListener {
            fun onItemClick(position: Int, holder: SuperViewHolder, model: TOListBean)
        }

    }
}